//
//  CollectViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/5.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "CollectViewController.h"
#import "CollectView.h"
#import "HelpCenterViewController.h"
#import "SelectIndustyViewController.h"
#import <Masonry.h>
#import "AppDelegate.h"
#import "HNineCollectionViewCell.h"

//交易页面
#import "TradingProcessViewController.h"


#define WIDTH  [[UIScreen mainScreen]bounds].size.width

#define HEIGHT [[UIScreen mainScreen]bounds].size.height

@interface CollectViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)CollectView *cv;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UIButton *delBtn;
@property(nonatomic,strong)UIButton *payBtn;
@property(nonatomic,strong)UIButton *zeroBtn;

@property(nonatomic,strong)NSMutableArray *titleArray;

@property(nonatomic,strong)NSString* HoriginalAmount;
@property(nonatomic,strong)NSString *HnewAccount;

@end

@implementation CollectViewController


-(NSMutableArray *)titleArray
{
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

#pragma mark 初始化
-(void)loadView{
    [super loadView];
    self.cv = [[CollectView alloc] initWithFrame:self.view.bounds];
    self.view = self.cv;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.cv.backgroundView.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"刷卡收款";
    //右上角帮助
    [self createRightBtn];
    //选择行业
    [self.cv.selectIndustryBtn addTarget:self action:@selector(selectIndustryBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectIndustryBtnAction)];
    [self.cv.selectIndustryValueLab addGestureRecognizer:tap];
    self.cv.selectIndustryValueLab.userInteractionEnabled = YES;
    //收款卡提示
    [self.cv.collectCardBtn addTarget:self action:@selector(collectCardBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    //创建键盘
    [self createNum];
    
    //点击确认支付
    [self.payBtn addTarget:self action:@selector(payBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)payBtnAction
{
    TradingProcessViewController *tradingVC = [[TradingProcessViewController alloc] init];
    [self.navigationController pushViewController:tradingVC animated:YES];
}


-(void)createNum
{
    self.titleArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"0",].mutableCopy;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat with = WIDTH / 4.0;
    CGFloat higth = with - 20;
    layout.itemSize = CGSizeMake(with, higth);
    NSLog(@"%f%f",with,higth);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT - higth * 4 - 64, WIDTH - WIDTH / 4.0, higth * 4) collectionViewLayout:layout];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.cv addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor colorWithRed:238 / 255.0 green:238 / 255.0 blue:240 / 255.0 alpha:1];
    [self.collectionView registerClass:[HNineCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    UIView *horizontalLineView1 = [[UIView alloc] init];
    horizontalLineView1.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    horizontalLineView1.frame = CGRectMake(0, HEIGHT - higth * 4 - 64, WIDTH, 1);
    [self.view addSubview:horizontalLineView1];
    
    
    
    UIView *horizontalLineView2 = [[UIView alloc] init];
    horizontalLineView2.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    horizontalLineView2.frame = CGRectMake(0, HEIGHT - higth * 4 - 64 + higth, WIDTH - WIDTH / 4.0, 1);
    [self.view addSubview:horizontalLineView2];
    
    UIView *horizontalLineView3 = [[UIView alloc] init];
    horizontalLineView3.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    horizontalLineView3.frame = CGRectMake(0, HEIGHT - higth * 4 - 64 + higth * 2, WIDTH - WIDTH / 4.0, 1);
    [self.view addSubview:horizontalLineView3];
    
    
    UIView *horizontalLineView4 = [[UIView alloc] init];
    horizontalLineView4.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    horizontalLineView4.frame = CGRectMake(0, HEIGHT - higth * 4 - 64 + higth * 3, WIDTH - WIDTH / 4.0, 1);
    [self.view addSubview:horizontalLineView4];
    
    
    
    UIView *verticalLineView1 = [[UIView alloc] init];
    verticalLineView1.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    verticalLineView1.frame = CGRectMake(WIDTH / 4.0, HEIGHT - higth * 4 - 64,  1, higth *4);
    [self.view addSubview:verticalLineView1];
    
    
    UIView *verticalLineView2 = [[UIView alloc] init];
    verticalLineView2.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    verticalLineView2.frame = CGRectMake(WIDTH / 4.0 * 2, HEIGHT - higth * 4 - 64,  1, higth * 3);
    [self.view addSubview:verticalLineView2];
    

    UIView *verticalLineView3 = [[UIView alloc] init];
    verticalLineView3.backgroundColor = [UIColor colorWithRed:225 / 255.0 green:225 / 255.0 blue:225 / 255.0 alpha:1];
    verticalLineView3.frame = CGRectMake(WIDTH / 4.0 * 3, HEIGHT - higth * 4 - 64,  1, higth);
    [self.view addSubview:verticalLineView3];
    
    self.delBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.delBtn setImage:[UIImage imageNamed:@"icon_revoke"] forState:(UIControlStateNormal)];
    self.delBtn.frame = CGRectMake(with * 3, HEIGHT - higth * 4 - 64, with, higth);
    [self.view addSubview:self.delBtn];
    
    self.payBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.payBtn setTitle:@"确认\n收款" forState:(UIControlStateNormal)];
    [self.payBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    self.payBtn.titleLabel.numberOfLines = 0;
    self.payBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    self.payBtn.backgroundColor = [UIColor colorWithRed:91 / 255.0 green:124 / 255.0 blue:255 / 255.0 alpha:1];
    self.payBtn.frame = CGRectMake(with * 3, HEIGHT - higth * 4 - 64 + higth, with, higth * 3);
    [self.view addSubview:self.payBtn];
    
    self.zeroBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.zeroBtn.backgroundColor = [UIColor whiteColor];
    [self.zeroBtn setTitle:@"00" forState:(UIControlStateNormal)];
    [self.zeroBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    self.zeroBtn.titleLabel.numberOfLines = 0;
    self.zeroBtn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
    self.zeroBtn.titleLabel.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    self.zeroBtn.frame = CGRectMake(with + 1, HEIGHT - higth * 4 - 64 + higth * 3 + 1, with *2 - 1, higth - 1);
    [self.view addSubview:self.zeroBtn];
    
    [self.delBtn addTarget:self action:@selector(delBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.zeroBtn addTarget:self action:@selector(zerozeroBtnBtn) forControlEvents:(UIControlEventTouchUpInside)];
    
}
-(void)zerozeroBtnBtn
{
    if(self.cv.visableLab.text.length < 8){
        self.HnewAccount = [self addLastValueToAmount:self.HnewAccount LastValue:@"00"];
        self.cv.visableLab.text = self.HnewAccount;
    }
}
-(void)delBtnAction
{
    self.HnewAccount = [self removeLastValueFromAmount:self.HnewAccount];
    self.cv.visableLab.text = self.HnewAccount;
}
-(NSString*) removeLastValueFromAmount:(NSString*) amount{
    NSString* originalCentStr = [amount stringByReplacingOccurrencesOfString:@"." withString:@""];
    if ([originalCentStr intValue] == 0) {
        return @"0.00";
    }
    NSString* newCentStr = [originalCentStr substringToIndex:[originalCentStr length]-1];
    NSString* yuanStr = [self cent2Yuan:newCentStr];
    return yuanStr;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.cv.visableLab.text.length < 9){
        self.HoriginalAmount = [self.cv.visableLab text];
        self.HnewAccount = nil;
        switch (indexPath.row) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:{
                NSString *lastValueStr = [NSString stringWithFormat:@"%ld",(indexPath.row + 1) % 10];
                self.HnewAccount = [self addLastValueToAmount:self.HoriginalAmount LastValue:lastValueStr];
                self.cv.visableLab.text = self.HnewAccount;
                break;
            }
            default:
                break;
        }
    }
}
-(NSString*) addLastValueToAmount:(NSString*) amount LastValue:(NSString*)value{
    NSString* originalCentStr = [amount stringByReplacingOccurrencesOfString:@"." withString:@""];
    NSString* newCentStr = [originalCentStr stringByAppendingString:value];
    NSString* yuanStr = [self cent2Yuan:newCentStr];
    return yuanStr;
}
-(NSString*)cent2Yuan:(NSString*)centStr{
    double cent = [centStr doubleValue];
    double yuan = cent/100;
    NSString*  yuanStr = [NSString stringWithFormat:@"%.2f",yuan];
    return yuanStr;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HNineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = self.titleArray[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}


-(void)collectCardBtnAction
{
    self.cv.backgroundView = [[UIView alloc] init];
    self.cv.backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    AppDelegate *delegate  = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.window addSubview:self.cv.backgroundView];    [self.cv.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo([[UIScreen mainScreen]bounds].size.height);
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
    
    self.cv.arrowImv = [[UIImageView alloc] init];
    self.cv.arrowImv.image = [UIImage imageNamed:@"more_icon_arrow"];
    [self.cv.backgroundView addSubview:self.cv.arrowImv];
    [self.cv.arrowImv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.cv.collectCardBtn);
        make.top.equalTo(self.cv.collectCardBtn.mas_bottom).offset(3);
    }];
    
    self.cv.popCollectCardView = [[UIView alloc] init];
    self.cv.popCollectCardView.layer.cornerRadius = 8;
    self.cv.popCollectCardView.backgroundColor = [UIColor whiteColor];
    [self.cv.backgroundView addSubview:self.cv.popCollectCardView];
    [self.cv.popCollectCardView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.cv.collectCardLab.mas_right).offset(-5);
        make.top.equalTo(self.cv.arrowImv.mas_bottom);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(140);
    }];
    
    self.cv.tipsLab = [[UILabel alloc] init];
    self.cv.tipsLab.text = @"什么是收款卡?";
    self.cv.tipsLab.font = [UIFont boldSystemFontOfSize:14];
    self.cv.tipsLab.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    [self.cv.popCollectCardView addSubview:self.cv.tipsLab];
    [self.cv.tipsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(20);
    }];
    
    self.cv.tipsValueLab = [[UILabel alloc] init];
    self.cv.tipsValueLab.text = @"收款卡是用于信用卡收款或刷卡交易时，打款至您银行卡账户，必须绑定本人的银行储蓄卡！";
    self.cv.tipsValueLab.numberOfLines = 0;
    self.cv.tipsValueLab.font = [UIFont systemFontOfSize:13];
    self.cv.tipsValueLab.textColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1];
    [self.cv.popCollectCardView addSubview:self.cv.tipsValueLab];
    [self.cv.tipsValueLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.equalTo(self.cv.tipsLab.mas_bottom).offset(10);
        make.right.mas_equalTo(-15);
    }];
    
    
    
    //增加手势，点击空白处退出收款卡弹出View
    UITapGestureRecognizer *collectCardtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(collectCardtapAction:)];
    [self.cv.backgroundView addGestureRecognizer:collectCardtap];
    
}
-(void)selectIndustryBtnAction
{
    NSLog(@"点击智能推荐");
    SelectIndustyViewController *selectVC = [[SelectIndustyViewController alloc] init];
    [self.navigationController pushViewController:selectVC animated:YES];
    
    //接收从下级页面传过来的行业
    selectVC.selectIndustyBlock = ^(NSString * _Nonnull Value) {
        self.cv.selectIndustryValueLab.text = Value;
    };
}
-(void)createRightBtn
{
    UIButton *rightBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    rightBtn.frame = CGRectMake(0, 0, 40, 20);
    [rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    [rightBtn setTitle:@"帮助" forState:(UIControlStateNormal)];
    [rightBtn setTitleColor:[UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1] forState:(UIControlStateNormal)];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
}
-(void)rightBtnAction
{
    HelpCenterViewController *helpVC = [[HelpCenterViewController alloc] init];
    [self.navigationController pushViewController:helpVC animated:YES];
}

-(void)collectCardtapAction:(UITapGestureRecognizer*)gap
{
    CGPoint point = [gap locationInView:self.cv.backgroundView];
    point = [self.cv.popCollectCardView.layer convertPoint:point fromLayer:self.cv.backgroundView.layer];
        if (![self.cv.popCollectCardView.layer containsPoint:point]) {
            [self.cv.backgroundView removeFromSuperview];
        }
}

@end
